﻿//Split Panel Script
//Created by TaoJianYang

function bindEvent(obj, evt, fun) {
    if (window.addEventListener) {
        obj.addEventListener(evt, function (e) { fun(e); }, false);
    } else {
        obj.attachEvent('on' + evt, fun);
    }
}

function SplitPanel(panel1, panel2, split, panel1MinSize, panel2MinSize) {
    if (panel1 && panel2 && split) {
        this.Panel1 = panel1;
        this.Panel2 = panel2;
        this.panelSplit = split;
        this.SplitWidth = this.panelSplit.offsetWidth;
        this.Panel1MinSize = panel1MinSize ? panel1MinSize : 200; // 最小宽度 或者 高度
        this.Panel2MinSize = panel2MinSize ? panel2MinSize : 500; // 最小宽度 或者 高度
        this.MouseDown = 0;
        this.init();
    }
};

SplitPanel.prototype.init = function () {
    var sp = this;
    var splitPanel = sp.panelSplit;

    bindEvent(document.body, 'mouseup', function (e) { sp.MouseDown = 0; });
    bindEvent(document.body, 'mouseleave', function (e) { sp.MouseDown = 0; });

    splitPanel.onmousedown = function (e) {
        sp.MouseDown = 1;
        e = e || event;
        sp.P = sp.T == 'H' ? (e.x || e.pageX) : (e.y || e.pageY);
    };
    bindEvent(document.body, 'mousemove', function (e) {
        if (sp.MouseDown) {
            e = e || event;
            p = e.x || e.pageX;
            var w1 = p - sp.SplitWidth;
            var w2 = sp.Panel2.offsetWidth - (p - (w1 + sp.SplitWidth));

            sp.Panel1.style.width = w1 - 2 + "px";
            //alert(sp.Panel2.style.left);
            sp.panelSplit.style.left = w1 + "px";
            sp.Panel2.style.left = w1 + sp.SplitWidth + "px";
            sp.Panel2.style.width = sp.Panel2.offsetWidth - (p - (w1 + sp.SplitWidth)) + "px";
            alert(sp.Panel2.style.width);
        }
    });

};

var sp = new SplitPanel($('navigation'), $('content'), $('split'), 120, 500);